package com.xingkeduo.controller.cms.activity;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xingkeduo.controller.base.ActivityBaseController;
import com.xingkeduo.model.param.ActivityParam;
import com.xingkeduo.model.param.ActivityParam.ActivityModeEnum;
import com.xingkeduo.model.param.ActivityParam.ActivityTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingCouponStatusEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingReceiveTypeEnum;
import com.xingkeduo.model.param.MarketingParam.MarketingStatusEnum;
import com.xingkeduo.service.cms.activity.ActivityMarketingTemplateService;
import com.xingkeduo.util.Constant;
import com.xingkeduo.util.tool.ResponseMessage;

/**
 * 
 * @ClassName: StoreTemplateCmsController
 * @Description: 门店活动相关-后台接口
 * @author lixin
 * @date May 3, 2019 3:12:54 PM
 *
 */
@RestController
@RequestMapping(Constant.CMS_URL_PREFIX + "/activity/store")
public class StoreTemplateCmsController extends ActivityBaseController {

	@Autowired
	ActivityMarketingTemplateService activityMarketingTemplateService;

	@RequestMapping("/addStoreActivityTemplate")
	public ResponseMessage addStoreActivityTemplate(@RequestBody ActivityParam storeActivityTemplate) {
		storeActivityTemplate.setActivityMode(ActivityModeEnum.STORE.getCode());
		storeActivityTemplate.setStatus(MarketingStatusEnum.DOWN.getCode());
		try {
			this.initParams(storeActivityTemplate);
			return activityMarketingTemplateService.saveOrUpdateActivityByTemplate(storeActivityTemplate);
		} catch (Exception e) {
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/getStoreActivityTemplateDetail")
	public ResponseMessage getStoreActivityTemplateDetail(Long storeActivityTemplateId) {
		return activityMarketingTemplateService.queryActivityById(storeActivityTemplateId);
	}

	@RequestMapping("/updateStoreActivityTemplate")
	public ResponseMessage updateStoreActivityTemplate(@RequestBody ActivityParam storeActivityTemplate) {
		storeActivityTemplate.setActivityMode(ActivityModeEnum.STORE.getCode());
		try {
			this.initParams(storeActivityTemplate);
			return activityMarketingTemplateService.saveOrUpdateActivityByTemplate(storeActivityTemplate);
		} catch (Exception e) {
			return ResponseMessage.error(e.getMessage(), 500);
		}
	}

	@RequestMapping("/auditStoreActivityTemplate")
	public ResponseMessage auditStoreActivityTemplate(Long storeActivityTemplateId, String status) {
		if (storeActivityTemplateId == null) {
			return ResponseMessage.error("活动id不合法", 500);
		}
		if (MarketingStatusEnum.parseStatus(Integer.parseInt(status)).equals(MarketingStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("活动状态不合法", 500);
		}
		ActivityParam storeActivityTemplate = new ActivityParam();
		storeActivityTemplate.setActivityMode(ActivityModeEnum.STORE.getCode());
		storeActivityTemplate.setId(storeActivityTemplateId);
		storeActivityTemplate.setStatus(Integer.parseInt(status));
		this.initUserInParams(storeActivityTemplate);
		try {
			return activityMarketingTemplateService.updateActivityStatusById(storeActivityTemplate);
		}catch(Exception e) {
			return ResponseMessage.error(e.getMessage(),500);
		}
	}

	@RequestMapping("/getStoreActivityTemplateList")
	public ResponseMessage getStoreActivityTemplateList(ActivityParam storeActivityTemplate) {
		storeActivityTemplate.setActivityMode(ActivityModeEnum.STORE.getCode());
		// 活动类型
		if (StringUtils.isNotBlank(storeActivityTemplate.getActivityType())
				&& ActivityTypeEnum.parse(storeActivityTemplate.getActivityType()).equals(ActivityTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("活动类型不合法", 500);
		}
		// 领取范围
		if (storeActivityTemplate.getReceiveType() != null && MarketingReceiveTypeEnum
				.parseStatus(storeActivityTemplate.getReceiveType()).equals(MarketingReceiveTypeEnum.UNKNOWN)) {
			return ResponseMessage.error("活动领取范围不合法", 500);
		}
		// 活动状态
		if (StringUtils.isNotBlank(storeActivityTemplate.getActivityStatus()) && MarketingCouponStatusEnum
				.parse(storeActivityTemplate.getActivityStatus()).equals(MarketingCouponStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("活动上下架状态不合法", 500);
		}
		// 上下架
		if (storeActivityTemplate.getStatus() != null && MarketingStatusEnum
				.parseStatus(storeActivityTemplate.getStatus()).equals(MarketingStatusEnum.UNKNOWN)) {
			return ResponseMessage.error("活动上下架状态不合法", 500);
		}
		return activityMarketingTemplateService.queryActivityByTemplate(storeActivityTemplate);
	}
}
